var mongoose=require('mongoose');
const User=require('../models/user.model');
exports.create=function(req,res,next){
    const user=new User(req.body);
    user.save().then(data=>{
        res.json(data)
    })
};
exports.updata=function(req,res,next){
    const id=req.params.id;
    User.findByIdAndUpdate(id,{$set:req.body},{new:false}).then(data=>{
        res.json(data)
    });
}
exports.remove=function(req,res,next){
    const id=req.params.id;
    User.findByIdAndRemove(id,(err,data)=>{
        res.json({message:'delete success'})
    });
}
exports.list=function(req,res,next){
    var page=req.body.page?req.body.page:1;
    var rows=req.body.rows?req.body.rows:10;
    var queryCondition={};
    if(req.params.condition&&req.params.condition.trim().length>0){
        condition=req.params.condition;
        queryCondition={
            name:new RegExp(condition)
        }
    }
    console.log(queryCondition,page,rows)
    User.paginate(queryCondition,{page:+page,limit:+rows},function(err,result){
        console.log(result);
        result.rows=result.docs;
        delete result.docs;
        res.json(result)
    })
}
exports.getdata=function(req,res,next){
    const id=req.params.id;
    User.findById(id,(err,data)=>{
        res.json(data);
        console.log(data)
    })
}